//
//  VistorView.swift
//  weiboDemo
//
//  Created by zq on 2020/10/30.
//

import UIKit

class VistorView: UIView {
    
    private lazy var bgImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var houseImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var registButton : UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.darkGray, backgroundImageName: "common_button_white_disable")
    private lazy var LoginButton : UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.darkGray, backgroundImageName: "common_button_white_disable")
    private lazy var tipLable : UILabel = UILabel.cz_label(withText: "登录以后,关注一些人看看有什么惊喜", fontSize: 14, color: UIColor.darkGray)
    var visitorInfo : [String : String]? {
        didSet{
            guard let imgName = visitorInfo?["imgName"],
                  let hintText = visitorInfo?["hintText"] else {
                return
            }
            if imgName == "" && hintText == "" {
                rotateBgImg()
                return
            }
            bgImageView.isHidden = true
            houseImageView.image = UIImage(named: imgName)
            tipLable.text = hintText
        }
    }

    override init(frame:CGRect) {
        super.init(frame: frame)
        setUpUI()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //旋转图标
    func rotateBgImg() -> () {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 10
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false
        bgImageView.layer.add(anim, forKey: nil)
    }
    
}

extension VistorView{
    func setUpUI() -> () {
        backgroundColor = UIColor.white
        //添加控件
        addSubview(bgImageView)
        addSubview(houseImageView)
        addSubview(registButton)
        addSubview(LoginButton)
        addSubview(tipLable)
        tipLable.textAlignment = .center
        
        //取消autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //尝试使用自动布局,
        addConstraint(NSLayoutConstraint(item: bgImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: bgImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: -80))
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .centerX, relatedBy: .equal, toItem: bgImageView, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .centerY, relatedBy: .equal, toItem: bgImageView, attribute: .centerY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .centerX, relatedBy: .equal, toItem: bgImageView, attribute: .centerX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .centerY, relatedBy: .equal, toItem: bgImageView, attribute: .centerY, multiplier: 1, constant: 130))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1, constant: -80))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: .top, relatedBy: .equal, toItem: tipLable, attribute: .bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: .left, relatedBy: .equal, toItem: tipLable, attribute: .left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 120))
        
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: .top, relatedBy: .equal, toItem: tipLable, attribute: .bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: .right, relatedBy: .equal, toItem: tipLable, attribute: .right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: LoginButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 120))
        
       
        
    }
}
